package com.example.stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 函数式数据处理(流)
 * Created by butterfly on 2018/7/3.
 */
public class StreamDemo {

    public static void main(String[] args) {
        List<Dish> menu = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH) );
        //1.过滤卡路里大于400的 2.把dish元素换成name元素  3,取头3个 4.收集到list中
        menu.stream().filter(dish -> dish.getCalories()>400).map(dish -> dish.getName()).limit(3).collect(Collectors.toList());
        menu.stream().peek(dish -> {
            dish.setCalories(22);
            System.out.println(dish.getName());
        }).collect(Collectors.toList());//peek不对元素进行任何操作的话，不会执行，同时需要终结操作
        System.out.println("==============");
        menu.stream().forEach(dish -> System.out.println(dish.getName()));
        System.out.println("==============");
        menu.stream().filter(dish -> dish.getType().equals(Dish.Type.MEAT)).limit(2).forEach(dish -> {
            dish.setName("hahaha");
            System.out.println(dish.getName());
        });
//        Stream<Dish> s = menu.stream();
//        s.forEach(System.out::println);
//        s.forEach(System.out::println);//流只能遍历一次，被消耗了就需要重新获取
        long count = menu.stream().distinct().limit(3).count();
        System.out.println(count);
        System.out.println("==========");
        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
        integers.stream().forEach(System.out::println);
        System.out.println("============");
        integers.stream().filter(i -> i%2==0 ).forEach(System.out::println);

    }
}
